package org.apache.rmiloader;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.PropertyConfigurator;
import org.apache.rmiloader.action.RmiReport;
import org.apache.rmiloader.kernel.RmiBounder;
import org.apache.rmiloader.util.RmiLoaderConfig;

/**
 * Servlet implementation class RmiClassLoader
 */
public class RmiClassLoader extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public RmiClassLoader() {
        super();
    }

    public void init(ServletConfig config) throws ServletException {
    	super.init(config);
    	
    	// Load log4j configuration
    	 ClassLoader cl = Thread.currentThread().getContextClassLoader();
         java.io.InputStream is = cl.getResourceAsStream("log4j.properties");
         if (is != null) {
        	 Properties prop = new Properties();
             try { prop.load(is); } catch (IOException e) { e.printStackTrace(); }
             PropertyConfigurator.configure(prop);
         }
         
         // Load rmiloader configuration
         RmiLoaderConfig.setAppHome(getServletContext().getRealPath("/"));
         RmiLoaderConfig appConfig = RmiLoaderConfig.getInstance();
         
         // Bound RMI Service
         long start = System.currentTimeMillis();
         RmiBounder rmi = new RmiBounder();
         String host = appConfig.getProperty(RmiLoaderConfig.RMI_HOST);
         int port = Integer.parseInt(appConfig.getProperty(RmiLoaderConfig.RMI_PORT));
         rmi.bound(host, port);
         long end = System.currentTimeMillis();
         RmiReport.getInstance().timeLoaded = end - start;
	}

}
